import { Component, OnInit } from '@angular/core';
import {Router,ActivatedRoute} from "@angular/router";
import {RollService} from "../../../my-service/roll.service";
import {UtilService} from "../../../my-service/util.service";
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-class-notices',
  templateUrl: './class-notices.component.html',
  styleUrls: ['./class-notices.component.css'],
})
export class classNoticesComponent implements OnInit {
 classNotices = {
   classGuid:null,
   stuGuid:null,
   notices:null,//所有的公告
   noReadLength:0,//未读公告的数量
   contentMaxHeight:60,//内容默认显示的高度
   contentArr:null,//存放通知正文部分的文本节点；
   isLoading:true,//是否显示加载画面
   noNotices:true,//是否有公告数据
   showMore:function () {
     this.notices.forEach((notice,index)=>{
        notice.isShowMore1 = false;
         if(this.contentArr[index].clientHeight>this.contentMaxHeight){
           notice.isShowMore = true;
         }else {
           notice.isShowMore = false;
         }
     })
   },/*是否显示展开按钮*/
   _constructor:function () {
     for (let notice of this.notices){
       if(notice.HaveRead === 0){
         this.noReadLength++;
       }
       continue;
     }
     this.getContentArr();
   },
   getContentArr:function () {
     setTimeout(()=>{
       this.contentArr = document.getElementsByClassName('notice_content');
       this.showMore();
     },0);
   }
 };

  constructor(
    private routerInfo:ActivatedRoute,
    private rollserve:RollService,
    private utilserve:UtilService,
    private msg:NzMessageService
  ) { }
  /*获取用户Guid*/
getUserGuid(){
    this.utilserve.getUserInfo().then(res=>{
      if(res.status==1){
        this.classNotices.stuGuid = res.userGuid;
        this.getNoticeList(this.classNotices.classGuid,this.classNotices.stuGuid);
      }
    })
}
/*获取通知列表*/
getNoticeList(classGuid,stuGuid){
  this.rollserve.getNoticeList(classGuid,stuGuid).then(res=>{
    this.classNotices.isLoading =false;
      if(res.status == 1){
         this.classNotices.notices = res.data;
        if(this.classNotices.notices.length>0){
          this.classNotices._constructor();
          this.classNotices.noNotices = false;
        }
      }else {
        this.msg.error('获取班级通知失败，请刷新重试')
      }
  })
}
  ngOnInit() {
    this.classNotices.classGuid= this.routerInfo.snapshot.params.classGuid;
    this.getUserGuid();
  }
/*用户读取通知*/
  onRead(notice){
    if(notice.HaveRead === 0){
    let data = {
      FK_NoticeGuid:notice.PK_NoticeGuid,
      FK_UserGuid:this.classNotices.stuGuid
    };
    this.rollserve.postReadNotice(data).then(res=>{
      console.log(res);
      if(res.status ==1){
        notice.HaveRead =1;
        this.classNotices.noReadLength-=1;
      }
    })
    }else {
      return;
    }
  }
}
